package com.lottery.im.module.service.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.BaseObserver;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.api.result.entity.EmptyData;
import com.lottery.im.api.result.entity.integral.AccOutCondition;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.service.view.CashExchangeView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

/**
 * 作者：cyy
 * 时间：2018/9/26。
 */
public class CashExchangePresenter extends BaseFragmentPresenter<CashExchangeView> {
  public CashExchangePresenter(Activity activity,
      CashExchangeView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  String token = MyApplication.getInstance().getToken();
  String userId = MyApplication.getInstance().getUserInfo().getUserId();

  public void queryAccOutCondition(){
    ApiClient.getApiRetrofitInstance()
        .queryAccOutCondition(userId,token)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new ShowTipObserver<AccOutCondition>(activity) {
          @Override public void onSuccess(AccOutCondition accOutCondition) {
            iView.queryAccOutConditionSuccess(accOutCondition);
          }
        });
  }

  public void userAccOut(String outPassword,String bankId,String outAmount){
    ApiClient.getApiRetrofitInstance()
        .userAccOut(userId,token,outPassword,bankId,outAmount,userId)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseObserver<EmptyData>() {
          @Override public void onStart() {
            showLoadingProgress("正在加载");
          }

          @Override public void onSuccess(EmptyData emptyData) {
            closeLoadingProgress();
            iView.userAccOutSuccess();
          }

          @Override public void onFail(String code, String msg) {
            closeLoadingProgress();
            iView.userAccOutFail(msg);
          }

          @Override public void onException(String msg) {
            closeLoadingProgress();
            iView.userAccOutFail(msg);
          }

          @Override public void onFinish() {
            closeLoadingProgress();
          }
        });
  }
}
